package com.yk.mr.service.cluster.impl;

import com.yk.mr.dao.cluster.api.IClusterDao;
import com.yk.mr.dao.common.api.ICommonDAO;
import com.yk.mr.model.cluster.ClusterModel;
import com.yk.mr.service.cluster.api.IClusterService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;

/**
 * Project Name:cykspark
 * File Name:ClusterServiceImpl
 * Package Name:com.yk.spark.service.cluster.impl
 * Date:2017/9/20 11:27
 * Author:zhangju
 * Description:
 * Copyright (c) 2017, 重庆云凯科技有限公司 All Rights Reserved.
 */

@Service("clusterService")
public class ClusterServiceImpl implements IClusterService{

    private final Log log= LogFactory.getLog(ClusterServiceImpl.class);
    @Resource(name = "commonDAO")
    private ICommonDAO commonDAO;

    @Resource(name = "clusterDao")
    private IClusterDao clusterDao;
    /**
     * 查询所有集群信息
     *
     * @return 集群信息列表
     */
    @Override
    public List<ClusterModel> queryAll() {
        return this.clusterDao.selectAll();
    }

    /**
     * 分页查询集群信息
     *
     * @param start 起始位置
     * @param limit 最大结果数
     * @return 集群信息列表
     */
    @Override
    public List<ClusterModel> query(int start, int limit) {
        return this.clusterDao.select(start,limit);
    }

    /**
     * 查询集群信息记录总数
     *
     * @return 集群信息记录总数
     */
    @Override
    public Long queryTotal() {
        return  this.clusterDao.total();
    }

    /**
     * 搜索集群信息
     *
     * @param clusterName 集群名称
     * @param ip          集群ip地址
     * @return 集群信息列表
     */
    @Override
    public List<ClusterModel> search(String clusterName, String ip) {
        ClusterModel model=new ClusterModel();
        model.setClusterName(clusterName);
        model.setIp(ip);
        return this.clusterDao.search(model);
    }

    /**
     * 查询集群信息
     *
     * @param clusterId 集群唯一标识
     * @return 集训信息
     */
    @Override
    public ClusterModel query(String clusterId) {
        ClusterModel model=new ClusterModel();
        model.setClusterId(clusterId);
        return this.clusterDao.select(model);
    }

    /**
     * 保存集群信息
     *
     * @param clusterName 集群名
     * @param ip          集群ip地址
     * @param port        集群端口
     * @param username    用户名
     * @param password    用户密码
     * @return 操作反馈
     */
    @Override
    public boolean saveCluster(String clusterName, String ip, int port, String username, String password) {
        ClusterModel model=new ClusterModel();
        model.setClusterName(clusterName);
        model.setIp(ip);
        model.setPort(port);
        model.setUserName(username);
        model.setUserPassword(password);
        model.setCreateTime(Timestamp.from(Instant.now()));
        model.setUpdateTime(Timestamp.from(Instant.now()));
        return this.commonDAO.save(model);
    }

    /**
     * 更新集群信息
     *
     * @param clusterId   集群唯一标识
     * @param clusterName 集群名
     * @param ip          集群ip地址
     * @param port        集群端口
     * @param username    用户名
     * @param password    用户密码
     * @return 操作反馈
     */
    @Override
    public boolean updateCluster(String clusterId, String clusterName, String ip, int port, String username, String password) {
        ClusterModel model=new ClusterModel();
        model.setClusterId(clusterId);
        model=this.clusterDao.select(model);
        model.setClusterName(clusterName);
        model.setIp(ip);
        model.setPort(port);
        model.setUserName(username);
        model.setUserPassword(password);
        return this.commonDAO.update(model);
    }

    /**
     * 删除集群信息
     *
     * @param clusterId 集群唯一标识
     * @return 操作反馈
     */
    @Override
    public boolean deleteCluster(String clusterId) {
        ClusterModel model=new ClusterModel();
        model.setClusterId(clusterId);
        return this.commonDAO.delete(model);
    }

    /**
     * 批量删除集群信息
     *
     * @param clustersId 集群唯一标识列表
     * @return 操作反馈
     */
    @Override
    public boolean deleteBatchCluster(List<String> clustersId) {
        boolean state=false;
        try (Session session = this.commonDAO.getNewSession()) {
            session.beginTransaction();
            for (String clusterId : clustersId) {
                ClusterModel model = new ClusterModel();
                model.setClusterId(clusterId);
                session.delete(model);
            }
            session.getTransaction().commit();
            state = true;
        } catch (Exception e) {
            this.log.error("batch delete error", e);
        }
        return state;
    }
}
